document.addEventListener('click', event => {
    changeActiveTab(event);
})

if (document.querySelector('.author-profile')) {
    displayContentsOfSelectedTab();
}



function changeActiveTab(event) {
    if (!event.target.closest('[data-active-author-content-tab]')) return;

    removeAllActiveTabs();
    event.target.closest('[data-active-author-content-tab').dataset.activeAuthorContentTab = 'true';
    displayContentsOfSelectedTab()
}

function displayContentsOfSelectedTab() {
    const selectedTab = getSelectedTab();

    if (selectedTab.classList.contains('authors-book')) {
        displayContentsOfBookListTab();
    } else if(selectedTab.classList.contains('about-author')) {
        displayInformationAboutAuthor();
    } else if (selectedTab.classList.contains('quotes-from-authors-book')) {
        displayQuotesOfAuthorsBooks();
    }

}

function displayQuotesOfAuthorsBooks() {
    // Показываем цитаты книг автора

    const url = document.location.href + '?quotes_from_authors_book=true';
    base.sendAjaxRequest(url, 'GET')
        .then(result => {
            insertData(result)
        })


    // НИЖЕ ОПРЕДЕЛЕНИЕ ФУНКЦИЙ

    function insertData(data) {
        const listOfQuotes = Object.values(data);
        const wrapper = document.createElement('div');
        wrapper.className = 'contents-of-tab quotes-from-authors-books';

        for (const quote of listOfQuotes) {
            const link_to_book = '/book/' + quote.slug_of_book;
            const link_to_book_quote = link_to_book + `/?page=${quote.page_number}`;
            const quoteHTML = `
                <div class="quote-from-authors-book-tab">
                    <a href="${link_to_book_quote}" class="link-to-book-quote-from-authors-book-tab">
                        <div class="content-quote-from-authors-book-tab">
                            ${quote.content}
                        </div>
                        <div class="additional-information-quote-from-authors-book-tab">
                            <span>${quote.page_number} стр.</span>
                            <span>/</span>
                            <a href="${link_to_book}">${quote.title_of_book}</</a>
                        </div>
                    </a>
                </div>
            `
            wrapper.insertAdjacentHTML('beforeend', quoteHTML);
        }

        document.querySelector('.authors-content-main').innerHTML = '';
        document.querySelector('.authors-content-main').insertAdjacentElement('beforeend', wrapper);
    }
}

function displayInformationAboutAuthor() {
    // Отображаем информацию об авторе

    
    const url = document.location.href + '?description_author=true';
    base.sendAjaxRequest(url, 'GET')
        .then(result => {
            insertData(result)
        })

    // НИЖЕ ОПРЕДЕЛЕНИЕ ФУНКЦИЙ

    function insertData(data) {
        const descriptionOfAuthor = data['description_of_author'];
        const wrapper = document.createElement('div');
        wrapper.className = 'contents-of-tab information-about-author-tab';
        wrapper.innerHTML = `
            <p>${descriptionOfAuthor}</p>
        `

        document.querySelector('.authors-content-main').innerHTML = '';
        document.querySelector('.authors-content-main').insertAdjacentElement('beforeend', wrapper);
    }
}

function displayContentsOfBookListTab() {
    const listOfBooks = getListOfBooks();


    // НИЖЕ ОПРЕДЕЛЕНИЕ ФУНКЦИЙ

    function getListOfBooks() {
        const url = document.location.href + '?list_of_books=true';
        base.sendAjaxRequest(url, 'GET')
            .then(result => {
                insertData(result);
            })
    }

    function insertData(data) {
        const books = Object.values(data);
        const wrapper = document.createElement('div');
        wrapper.className = 'contents-of-tab contents-of-book-tab';

        for (const book of books) {
            const linkToBook = '/book/' + book[1]
            const bookHTML = `
                <a href="${linkToBook}" class="book-for-list-tab">
                    <img src="/media/${book[0]}">
                </a>
            `
            wrapper.insertAdjacentHTML('beforeend', bookHTML);
        }

        document.querySelector('.authors-content-main').innerHTML = '';
        document.querySelector('.authors-content-main').insertAdjacentElement('beforeend', wrapper);
    }
}

function getSelectedTab() {
    return document.querySelector('[data-active-author-content-tab="true"]');
}

function removeAllActiveTabs() {
    // Убираем все активные вкладки

    const allTabs = Array.from(document.querySelectorAll('[data-active-author-content-tab]'));

    for (const tab of allTabs) {
        tab.dataset.activeAuthorContentTab = 'false';
    }
}